﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HRM.BLL;
using HRM.DAL.DAO;
using HRM.DAL.Report;

namespace HRM.UI.HRManagerUI.EvaluationUI
{
    public partial class EvaluateEmployee : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetProjects();
                selectTable.Visible = false;
                evaluationReportGvr.Visible = false;
            }
        }

        private void GetProjects()
        {
         //   int empId = (int)Session["Id"];
            List<Project> projects = new List<Project>();
            ProjectManager manager = new ProjectManager();
            projects = manager.GetAllProjects();
            selectEmployeeInProject.DataValueField = "Id";
            selectEmployeeInProject.DataTextField = "ProjectName";
            selectEmployeeInProject.DataSource = projects;
            selectEmployeeInProject.DataBind();
            selectEmployeeInProject.Items.Insert(0, "--Select Project--");
        }

        protected void SearchEmployee(object o, EventArgs e)
        {
            GetEmployeesInProject();
        }

        private void GetEmployeesInProject()
        {
            EmployeeManager empManager = new EmployeeManager();
            List<ViewEmployee> employees = new List<ViewEmployee>();
            Project project = new Project();
            project.Id = Convert.ToInt32(selectEmployeeInProject.Value);
            employees = empManager.SearchEmployeeByProject(project);
            searchEmpGrid.DataSource = employees;
            searchEmpGrid.DataBind();
            lblFondResult.ForeColor = Color.Green;
            lblFondResult.Text = "Total Employee Found :" + employees.Count.ToString();

        }

        protected void searchEmpGrid_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            searchEmpGrid.PageIndex = e.NewPageIndex;
            GetEmployeesInProject();

        }


        protected void searchEmpGrid_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("select", StringComparison.CurrentCultureIgnoreCase))
            {
                selectTable.Visible = true;
                lblEmpCode.ForeColor = Color.Green;
                lblEmpCode.BackColor = Color.Cornsilk;
                lblEmpCode.Font.Bold = true;
                lblEmpCode.Text = (e.CommandArgument).ToString();
                Code code = new Code();
                code.CodeName = lblEmpCode.Text;
                PopulateEvaluation(code);

            }
        }

        protected void EvaluateEmployeeForPromotion(object o, EventArgs e)
        {
            try
            {
                Evaluation evaluation = new Evaluation();
                Code code = new Code();
                Employee employee = new Employee();
                code.CodeName = lblEmpCode.Text;
                employee.EmpCode = code;
                evaluation.Employee = employee;
                PM pm = new PM();
                pm.Id = (int)Session["Id"];
                evaluation.Pm = pm;
                evaluation.Date = txtDate.Value;
                evaluation.TechnicalSkill = selectSkill.Value;
                evaluation.Communication = selectCommunication.Value;
                evaluation.Punctuality = selectPunctuality.Value;
                evaluation.Comments = txtRemarks.Value;

                EvaluationManager manager = new EvaluationManager();
                if (manager.EvaluateEmployee(evaluation))
                {
                    msgLbl.ForeColor = Color.Green;
                    msgLbl.Text = "Employee Evaluated Successfully";
                    PopulateEvaluation(code);
                    evaluationReportGvr.Visible = true;
                }
                else
                {
                    msgLbl.ForeColor = Color.Red;
                    msgLbl.Text = "Employee Not Evaluated , Try again";
                }
            }
            catch (Exception ex)
            {
                msgLbl.ForeColor = Color.Red;
                msgLbl.Text = ex.Message;
            }
        }

        private void PopulateEvaluation(Code code)
        {
            EvaluationManager manager = new EvaluationManager();
            SingleEmployeEvaluationReport report = new SingleEmployeEvaluationReport();
            report = manager.GetEmployeesEvaluation(code);
            List<SingleEmployeEvaluationReport> reports = new List<SingleEmployeEvaluationReport>();
            reports.Add(report);
            evaluationReportGvr.DataSource = reports;
            evaluationReportGvr.DataBind();
        }
    }
}